package com.htx.oldbookpavilion.service.bookinfoservice.impl;

import com.htx.oldbookpavilion.common.StandardPageData;
import com.htx.oldbookpavilion.entity.BookInfo;
import com.htx.oldbookpavilion.entity.BookInfoExample;
import com.htx.oldbookpavilion.entity.BookTypeDict;
import com.htx.oldbookpavilion.entity.BookTypeDictExample;
import com.htx.oldbookpavilion.mapper.BookInfoExtMapper;
import com.htx.oldbookpavilion.mapper.BookTypeDictMapper;
import com.htx.oldbookpavilion.service.bookinfoservice.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookInfoServiceImpl implements BookInfoService {

    @Autowired
    private BookInfoExtMapper bookInfoExtMapper;

    @Autowired
    private BookTypeDictMapper bookTypeDictMapper;

    /**
     * 按条件分页获取书籍信息
     * @param sPageData
     * @return
     */
    @Override
    public StandardPageData<BookInfo> getBooksByPage(StandardPageData<BookInfo> sPageData) {
        BookInfoExample bookInfoExample = new BookInfoExample();
        //处理查询参数，配置模糊查询
        setStandardPageParams(sPageData);
        List<BookInfo> bookInfoList = bookInfoExtMapper.queryUploadBooksByPage(sPageData);
        long total = bookInfoExtMapper.countByStandardPage(sPageData);
        sPageData.setTotal(total);
        sPageData.setRecords(bookInfoList);
        return sPageData;
    }

    /**
     * 获取标签数据
     * @return
     */
    @Override
    public List<BookTypeDict> getBookTypeData() {
        return bookTypeDictMapper.selectByExample(new BookTypeDictExample());
    }

    private void setStandardPageParams(StandardPageData<BookInfo> sPageData){
        if(sPageData.getSearchParam().getBookName() != null && !("").equals(sPageData.getSearchParam().getBookName())){
            sPageData.getSearchParam().setBookName("%" + sPageData.getSearchParam().getBookName() + "%");
        }
        if (sPageData.getSearchParam().getAuthor() != null && !("").equals(sPageData.getSearchParam().getAuthor())) {
            sPageData.getSearchParam().setAuthor("%" + sPageData.getSearchParam().getAuthor() + "%");
        }
        if (sPageData.getSearchParam().getType() != null && !("").equals(sPageData.getSearchParam().getType())){
            sPageData.getSearchParam().setType("%" + sPageData.getSearchParam().getType() + "%");
        }
        if (sPageData.getSearchParam().getParentType() != null && !("").equals(sPageData.getSearchParam().getParentType())){
            sPageData.getSearchParam().setParentType("%" + sPageData.getSearchParam().getParentType() + "%");
        }
    }
}
